// pages/news/index.js
const app = getApp();
import req from '../../utils/Request.js';
import appReq from '../../utils/AppRequest.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgList: [],
    tab: true,
    list: [],
    page: 1,
    newsType: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getImg();
    this.setData({
      newsType: this.data.tab ? 'flash' :'policy'
    })
    this.getNews()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.totalPages > this.data.page) {
      this.setData({
        page: this.data.page + 1
      });
      this.getNews();
    } else {
      wx.showToast({
        title: '没有更多信息了',
        icon: 'none',
        duration: 2000
      });
      return
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getImg: function() {
    req.get('/index/pageList.do',{
      newsType: "banner",
      page: 1,
      pageSize: 100000,
      cityId: wx.getStorageSync('cityId')
    }).then(res => {
      if (res.code == 1) {
        // let arr = [];
        // res.data.content.map(i => {
        //   arr.push(i.newsPicture)
        //   return i
        // })
        this.setData({
          imgList: res.data.content,
        })
        wx.hideLoading();
      } else {
        app.showErrorMessage(res, '加载失败');
        wx.hideLoading();
        return;
      }
    }).catch(res => {
      wx.hideLoading();
      wx.showToast({
        title: '网络异常',
        icon: 'none'
      });
      console.log(res);
    });
  },

  getNews: function(v) {
    let l = this.data.list;
    req.get('/index/pageList.do', {
      newsType: this.data.newsType,
      page: this.data.page,
      pageSize: 10,
      cityId: wx.getStorageSync('cityId')
    }).then(res => {
      if (res.code == 1) {
        this.setData({
          list: l.concat(res.data.content),
          totalPages: res.data.totalPages,
        })
        if (this.data.page === 1 && res.data.content.length === 0) {
          this.setData({
            list: [],
            totalPages: res.data.totalPages,
          })
        }
        wx.hideLoading();
      } else {
        app.showErrorMessage(res, '加载失败');
        wx.hideLoading();
        return;
      }
    }).catch(res => {
      wx.hideLoading();
      wx.showToast({
        title: '网络异常',
        icon: 'none'
      });
      console.log(res);
    });
  },

  changeTab:function() {
    this.setData({
      tab: !this.data.tab,
      page: 1,
      list: []
    })
    this.setData({
      newsType: this.data.tab ? 'flash' :'policy'
    })
    this.getNews()
  },
  goNewDetail: function(e) {
    let _index = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: '../newDetail/index?keyId=' + this.data.list[_index].keyId
    })
  },
  goImgDetail: function (e) {
    let _index = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: '../newDetail/index?keyId=' + this.data.imgList[_index].keyId
    })
  }
})